package com.student.management.root.web;

import com.student.management.root.bean.Users;
import com.student.management.root.service.UsersService;
import com.student.management.root.service.impl.UsersServiceImpl;
import com.student.management.root.utils.PageUtil;
import com.student.management.root.utils.UsersEnum;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@SuppressWarnings("all")
@WebServlet(urlPatterns = {"/power/user/users"})
public class UsersServlet  extends HttpServlet {
    private UsersService usersService=new UsersServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String method = req.getParameter("method");
        if("select".equals(method)){
            select(req,resp);
        }else if ("update".equals(method)){
            update(req, resp);
        }else if ("insert".equals(method)){
            insert(req, resp);
        }else if ("findbyid".equals(method)){
            findUserbyid(req, resp);
        }else if ("delete".equals(method)){
            delete(req, resp);
        }else {
            select(req,resp);
        }
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String userid = req.getParameter("userid");
        System.out.println("删除用户的userid： "+userid);
        int i = usersService.deleteById(Integer.valueOf(userid));
        PrintWriter writer = resp.getWriter();
        if (i>0){
            writer.print("<script>alert('delete success');location.href='/4StudentManagement/power/user/users';</script>");
        }else {
            writer.print("<script>alert('delete failed');location.href='/4StudentManagement/power/user/users';</script>");
        }
    }

    /**
     *
     * @param req
     * @param resp
     */
    private void insert(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String phone = req.getParameter("phone");
        String email = req.getParameter("email");
        String cardid = req.getParameter("cardid");
        String realname = req.getParameter("realname");
        String gender = req.getParameter("gender");
        String address = req.getParameter("address");
        String role = req.getParameter("role");

        Users users = new Users();
        if (role.equals(UsersEnum.ADMIN.value)){
            users.setRoleId(1);
        }else if(role.equals(UsersEnum.CLASSADMIN.value)){
            users.setRoleId(2);
        }else if(role.equals(UsersEnum.TEACHER.value)){
            users.setRoleId(4);
        }else if(role.equals(UsersEnum.STUDENT.value)){
            users.setRoleId(3);
        }else if(role.equals(UsersEnum.UNSELECTED.value)){
            users.setRoleId(3);
        }
        users.setLoginName(username);
        users.setPassWord(password);
        users.setRealName(realname);
        users.setSex(Integer.valueOf(gender));
        users.setCardId(cardid);
        users.setAddress(address);
        users.setPhone(phone);
        users.setEmail(email);

        System.out.println("新增的user数据： "+users.toString());
        int i = usersService.insertUser(users);
        PrintWriter printWriter = resp.getWriter();
        if (i>0){
            printWriter.print("<script>alert('Add success');location.href='/4StudentManagement/power/user/users';</script>");
        }else {
            printWriter.print("<script>alert('Add failed');location.href='/4StudentManagement/power/user/add.jsp';</script>");
        }


    }

    private void findUserbyid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userid = req.getParameter("userid");
        //根据主键查询学生信息
        UsersService service=new UsersServiceImpl();
        Users user = service.findById(Integer.parseInt(userid));

        req.setAttribute("user",user);
        req.getRequestDispatcher("edit.jsp").forward(req,resp);
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String uId = req.getParameter("uId");
        String loginName = req.getParameter("loginName");
        String password = req.getParameter("password");
        String realname = req.getParameter("realname");
        String gender = req.getParameter("gender");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String address = req.getParameter("address");
        String cardid = req.getParameter("cardid");
        String role = req.getParameter("role");
        Users users = new Users();
        if (role.equals(UsersEnum.ADMIN.value)){
            users.setRoleId(1);
        }else if(role.equals(UsersEnum.CLASSADMIN.value)){
            users.setRoleId(2);
        }else if(role.equals(UsersEnum.TEACHER.value)){
            users.setRoleId(4);
        }else if(role.equals(UsersEnum.STUDENT.value)){
            users.setRoleId(3);
        }else if(role.equals(UsersEnum.UNSELECTED.value)){
            users.setRoleId(3);
        }

        users.setUserId(Integer.valueOf(uId));
        users.setLoginName(loginName);
        users.setPassWord(password);
        users.setRealName(realname);
        users.setSex(Integer.valueOf(gender));
        users.setCardId(cardid);
        users.setAddress(address);
        users.setPhone(phone);
        users.setEmail(email);

        System.out.println("更新的user数据： "+users.toString());
        int i = usersService.updateUser(users);
        PrintWriter printWriter = resp.getWriter();
        if (i>0){
            printWriter.print("<script>alert('update success');location.href='/4StudentManagement/power/user/users'</script>");
        }else {
            printWriter.print("<script>alert('update failed');location.href='/4StudentManagement/power/user/users?method=findbyid&userid="+uId+"</script>");
        }
    }

    //查询数据（分页）
    protected void select(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收参数(分页参数+模糊查参数)
        String index = req.getParameter("index");
        int pageIndex=(index==null||index.length()==0)?1:Integer.parseInt(index);
        //2.调取service方法(1.查询数据列表的方法  2.查询总条数的方法)
        PageUtil pageUtil = new PageUtil();
        List<Users> usersList = usersService.getUsersList(pageIndex, pageUtil.getPageSize());
        int total = usersService.total();
        pageUtil.setTotal(total);
        pageUtil.setPageIndex(pageIndex);
        pageUtil.setDataList(usersList);

        //3.存值跳页面
        req.setAttribute("pi",pageUtil);
        req.getRequestDispatcher("list.jsp").forward(req,resp);



    }


}
